from app import create_app, db
from app.models import User, Major, Basicinfo

app = create_app()

# Shell上下文（调试用）
@app.shell_context_processor
def make_shell_context():
    return dict(db=db, User=User, Major=Major, Basicinfo=Basicinfo)

# 初始化管理员账号（首次运行时）
with app.app_context():
    db.create_all()
    old_admins = User.query.filter_by(role='admin').all()
    if old_admins:
        for admin in old_admins:
            db.session.delete(admin)
            db.session.commit()
    new_admin = User(username='sxh')
    new_admin.set_password('yesyes')
    new_admin.role = 'admin'
    db.session.add(new_admin)
    db.session.commit()

if __name__ == '__main__':
    app.run(debug=True)